package hl.demosample.ui.fragment.login

import android.os.Bundle
import android.widget.CompoundButton
import androidx.fragment.app.viewModels
import hl.demosample.R
import hl.demosample.app.appViewModel
import hl.demosample.app.base.BaseFragment
import hl.demosample.app.ext.checkMobileAll
import hl.demosample.app.ext.initClose
import hl.demosample.app.ext.showMessage
import hl.demosample.app.util.CacheUtil
import hl.demosample.app.util.LoginAndRegisterAnimation.inputAnimator
import hl.demosample.app.util.LoginAndRegisterAnimation.recovery
import hl.demosample.app.util.SettingUtil
import hl.demosample.app.util.SettingUtil.getOneColorStateList
import hl.demosample.databinding.FragmentLoginBinding
import hl.demosample.viewmodel.request.LoginRegisterViewModel
import hl.demosample.viewmodel.request.RequestLoginRegisterViewModel
import kotlinx.android.synthetic.main.fragment_login.*
import kotlinx.android.synthetic.main.include_toolbar.*
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.parseState
/*
* path: DemoSample 
* Author: 86133
* Create: 2021-06-02 14:52
* Changes (from 2021-06-02)
* Desc:
*/
//https://blog.csdn.net/qq_25193681/article/details/52005375
//位移:translationX、translationY、translationZ
//透明度:alpha,透明度全透明到不透明:0f->1f
//旋转:rotation,旋转一圈:0f->360f
//缩放：水平缩放scaleX,垂直缩放scaleY

class LoginFragment : BaseFragment<LoginRegisterViewModel, FragmentLoginBinding>() {

    private val requestLoginRegisterViewModel: RequestLoginRegisterViewModel by viewModels()

    override fun layoutId() = R.layout.fragment_login

    override fun initView(savedInstanceState: Bundle?) {

        mDatabind.viewmodel = mViewModel
        mDatabind.click = ProxyClick()

        toolbar.initClose("登录") {
            nav().navigateUp()
        }

        //设置颜色跟主题颜色一致
        appViewModel.appColor.value?.let {
            SettingUtil.setShapColor(tv_login, it)
            toolbar.setBackgroundColor(it)
            loginGoregister.setTextColor(it)
            progressBar2.indeterminateTintList = getOneColorStateList(appContext)
        }
    }

    override fun createObserver() {
        requestLoginRegisterViewModel.loginResult.observe(viewLifecycleOwner, { resultState ->
            parseState(resultState, {
                //登录成功 通知账户数据发生改变鸟
                CacheUtil.setUser(it)
                CacheUtil.setIsLogin(true)
                nav().navigateUp()
            }, {
                //登录失败
                recovery(layout_progress, input_layout)
                showMessage(it.errorMsg)
            })
        })
    }

    inner class ProxyClick {
        fun clear() {
            mViewModel.username.set("")
        }

        fun login() {
            when {
                mViewModel.username.get().isEmpty() -> showMessage("请填写账号")
                !checkMobileAll(mViewModel.username.get()) -> showMessage("请填写正确的手机号码")
                mViewModel.password.get().isEmpty() -> showMessage("请填写密码")
                else -> {
                    val mWidth = tv_login.measuredWidth
                    val mHeight = tv_login.measuredHeight
                    inputAnimator(input_layout, mWidth.toFloat(), mHeight.toFloat(), input_layout, layout_progress)
                    requestLoginRegisterViewModel.loginReq(mViewModel.username.get(), mViewModel.password.get())
                }
            }
        }

        fun goCancel() {
            recovery(layout_progress, input_layout)
        }

        fun goRegister() {
            nav().navigateAction(R.id.action_loginFragment_to_registerFrgment)
        }

        var onCheckedChangeListener =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->
                mViewModel.isShowPwd.set(isChecked)
            }
    }
}